package com.example.springboot.controller.business;

import com.alibaba.fastjson.JSONObject;
import com.example.springboot.aop.Release;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.blog.service.serviceImp.Test;
import com.example.springboot.mapper.blog.service.serviceImp.TokenService;
import com.example.springboot.mapper.blog.service.serviceImp.UserLoginServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
public class Login {

    @Autowired
    private UserLoginServiceImp userLoginService;
    @Autowired
    private Test Test;
//    @Autowired
//    private TokenService tokenService;
    @Resource(name="TokenService")
    private TokenService tokenService;

    @Autowired
    private RedisTemplate redisTemplate;



    @GetMapping("/test")
    public Object userLss(){
        for (int i = 0; i < 10; i++) {
            Test.executeAsyncTask(i);
            Test.executeAsyncTaskPlus(i);
        }
        System.out.println("完成");
        while (true){
            if(Test.getInts() == 10){
                System.out.println("輔助");
                break;
            }
        }
        return Test.getInts();
    }

    /**
     * @api {POST} /userLogin/  用户登录
     * @apiDeprecated 已经失效 write (#User:GetDetails)
     * @apiDescription 用于用户登录
     * @apiGroup userLogin
     * @apiParam {String} username 用户名(必填*)
     * @apiParam {String} pas 密码(必填*)
     * @apiSuccessExample 登录成功
     * {
     *  statusCode: 2000,
     *  mes: '查询成功',
     *  data: {
     *      profilePhotoUrl:"sasasa.png",  // 头像url
     *      username:"username",   // 账号
     *      nickname:"艾伦",  // 昵称
     *      token:"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9",  // 昵称
     *  }
     * }
     * @apiErrorExample 账号或者密码为错误
     * {
     *  statusCode: 2004,
     *  mes: '账号或者密码为错误'
     * }
     * @apiErrorExample 传入参数不能为空
     * {
     *  statusCode: 4004,
     *  mes: '传入参数不能为空'
     * }
     * @apiVersion 1.0.1
     */
    @Release
    @RequestMapping(value = "/userLogin")
    public Map userlogin(String username, String pas, HttpServletResponse response){
        Map mps = new HashMap<>();
        if(username != null && pas != null){
            List list = userLoginService.selectUser(username, pas);
            if (list.size() > 0){
                mps.put("statusCode",2000);
                mps.put("mes","查询成功");
                User user = (User)list.get(0);
                // 生成token
                JSONObject token = tokenService.createToken(user);
                // 设置token
                tokenService.createCacheUser(token);
                Map<String,Object> data = new HashMap<>();
                data.put("profilePhotoUrl","/profilePhotoImg/" + user.getProfilePhotoUrl());
                data.put("username",user.getUsername());
                data.put("token",token.get("token"));
                mps.put("data",data);
            }else{
                mps.put("statusCode",2004);
                mps.put("mes","账号或者密码为错误");
            }
        }else{
            mps.put("statusCode",4004);
            mps.put("mes","传入参数不能为空");
        }
        return mps;
    }
}
